package com.kevin.i18n.dynamic.configuration.component;

import com.kevin.i18n.dynamic.model.Language;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Locale;

@Component
public class I18nMessageSource extends AbstractMessageSource {

    @Resource
    private I18nDynamicAdapter i18nDynamicAdapter;

    @Override
    protected MessageFormat resolveCode(@Nullable String code, @Nullable Locale locale) {
        Language language = i18nDynamicAdapter.dynamicMessage(locale, code);
        return (null != language && StringUtils.isNotEmpty(language.getContent())) ?
                createMessageFormat(language.getContent(), locale) : null;
    }

}
